package cn.factorybox.item.controller;

import cn.factorybox.common.entity.Constant;
import cn.factorybox.common.entity.Result;
import cn.factorybox.item.annotation.ControllerLogAnnotation;
import cn.factorybox.item.annotation.Log;
import cn.factorybox.item.enums.OperationType;
import cn.factorybox.item.enums.OperationUnit;
import cn.factorybox.item.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-03-04 17:43
 */
@Api(tags = "旧的终端设备管理")
@RestController
@RequestMapping("device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @ControllerLogAnnotation(moduleName = "旧的终端设备管理", functionName = "获取设备列表")
    @ApiOperation(value = "获取设备列表",notes = "获取设备列表")
    @GetMapping("deviceData")
    public Result selectList() {
        Map<String,Object> map = deviceService.selectList();
        return Result.ok(map,"成功获取设备数据");
    }

    @ControllerLogAnnotation(moduleName = "旧的终端设备管理", functionName = "获取设备列表2")
//    @Log(detail = "获取设备列表2",level = 3,operationUnit = OperationUnit.DEVICE,operationType = OperationType.SELECT)
    @ApiOperation(value = "获取设备列表2", notes = "获取设备列表2")
    @GetMapping("list")
    public Result selectList2() {
        Map<String,Object> map = deviceService.selectList2();
        return Result.ok(map,"成功获取设备数据");
    }

    @ControllerLogAnnotation(moduleName = "旧的终端设备管理", functionName = "获取包含摄像头的设备列表")
    @ApiOperation(value = "获取包含摄像头的设备列表",notes = "获取包含摄像头的设备列表")
    @GetMapping("cameralist")
    public Result selectIncludeCamera() {
        Map<String,Object> map = deviceService.selectCamera();
        return Result.ok(map,"成功获取设备列表");
    }

    @ControllerLogAnnotation(moduleName = "旧的终端设备管理", functionName = "新增了一个设备")
    @Log(detail = "新增了一个设备",level = 2,operationUnit = OperationUnit.DEVICE,operationType = OperationType.INSERT)
    @ApiOperation(value = "新增设备",notes = "新增设备")
    @PostMapping("deviceData")
    public Result insertDevice(@RequestBody Map<String,Object> map) {
        deviceService.insert(map);
        return Result.ok("新增设备成功");
    }

    @ControllerLogAnnotation(moduleName = "旧的终端设备管理", functionName = "完成修改终端设备备注")
    @Log(detail = "完成修改终端设备备注",level = 2,operationUnit = OperationUnit.DEVICE,operationType = OperationType.UPDATE)
    @ApiOperation(value = "修改设备",notes = "修改设备")
    @PutMapping("deviceData")
    public Result updateDevice(@RequestBody Map<String,Object> map) {
        deviceService.update(map);
        return Result.ok("修改设备成功");
    }

    @ControllerLogAnnotation(moduleName = "旧的终端设备管理", functionName = "检查设备号是否存在")
    @ApiOperation(value = "检查设备号是否存在",notes = "检查设备号是否存在")
    @GetMapping("exist")
    public Result checkDevice(@RequestParam("deviceID") String fSerialNumber) {
        if (deviceService.checkDevice(fSerialNumber) > 0){
            return Result.ok("验证成功，此设备号满足条件");
        }
        return Result.error(Constant.SUCCEED_CODE,"不存在该设备");
    }

    @ControllerLogAnnotation(moduleName = "旧的终端设备管理", functionName = "获取连接信息")
    @ApiOperation(value = "获取连接信息", notes = "获取连接信息")
    @PostMapping("connect")
    public Result establishConnect(@RequestParam("deviceID") String fSerialNumber, @RequestParam("devicePass") String pass) {
        Map<String,Object> map = deviceService.getConnect(fSerialNumber,pass);
        if (map != null) {
            Map<String,Object> maps = new HashMap<>();
            maps.put("data",map);
            return Result.ok(maps,"验证连接成功");
        }
        return Result.error("连接失败");
    }

    @ControllerLogAnnotation(moduleName = "旧的终端设备管理", functionName = "完成删除终端设备")
    @Log(detail = "完成删除终端设备",level = 2,operationUnit = OperationUnit.DEVICE,operationType = OperationType.DELETE)
    @ApiOperation(value = "删除设备", notes = "删除设备")
    @DeleteMapping("deviceData")
    public Result deleteDevice(@RequestParam("id") String id) {
        deviceService.deleteDevice(id);
        return Result.ok("删除设备成功");
    }

}
